<?php
/**
 * Description: 清洗数据（Brand_content）
 * Created by PhpStorm.
 * User: congyukun
 * Date: 2021/3/4
 * Time: 16:50
 */

use Biz\model\brand\Content;

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';


$start = time();
$page = 1;
$pageSize = 1000;
$list = [];

//查询品牌名称
$brand_info = \Biz\model\brand\Brand::select(['id', 'brand_name', 'brand_alias'], ['uid[>]' => 0, 'status' => 1]);
$brand_info = array_column($brand_info, null, 'id');

do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    
    $where = [
        'brand_id[>]' => 0,
        'words[!]' => "",
        'LIMIT' => $limit,
    ];
    
    $list = Content::select(['id', 'brand_id', 'words'], $where);
    
    $data = [];
    
    foreach ($list as $row) {
        if (isset($brand_info[$row['brand_id']])) {
            
            //获取word数组
            $words_arr = json_decode($row['words'], true);
            if ($words_arr) {
                $content = $words_arr['content'] ?? [];
                $tag = $words_arr['tag'] ?? [];
                $words = array_merge($content, $tag);
                $brand_name = $brand_info[$row['brand_id']]['brand_name'] ?? '';
                $brand_alias = $brand_info[$row['brand_id']]['brand_alias'] ? json_decode($brand_info[$row['brand_id']]['brand_alias'],
                    true) : [];
                unset($words_arr['brand_name'], $words_arr['brand_alias']);
                //遍历words词
                $search_type = [];
                foreach ($words as $word) {
                    
                    //命中品牌
                    if ($brand_name) {
                        if (stripos($word, $brand_name) !== false) {
                            if (isset($words_arr['brand_name'])) {
                                if (!in_array($brand_name, $words_arr['brand_name'])) {
                                    $words_arr['brand_name'][] = $brand_name;
                                }
                            } else {
                                $words_arr['brand_name'][] = $brand_name;
                                $search_type[] = Content::SEARCH_TYPE_BRAND_NAME;
                            }
                        }
                    }
                    //命中品牌别称
                    if ($brand_alias) {
                        foreach ($brand_alias as $alias) {
                            if (stripos($word, $alias) !== false) {
                                if (isset($words_arr['brand_alias'])) {
                                    if (!in_array($alias, $words_arr['brand_alias'])) {
                                        $words_arr['brand_alias'][] = $alias;
                                    }
                                } else {
                                    $words_arr['brand_alias'][] = $alias;
                                    $search_type[] = Content::SEARCH_TYPE_BRAND_ALIAS;
                                }
                            }
                        }
                    }
                }
                $data['words'] = json_encode($words_arr, JSON_UNESCAPED_UNICODE);
                $data['search_type'] = implode(',', array_unique($search_type));
                
                if (!empty($data['search_type'])) {
                    var_dump($row['brand_id']);
                    var_dump($data);
                    Content::update($data, ['id' => $row['id']]);
                }
            }
        }
    }
    $page++;
} while ($list);
$end = time();
var_dump(date('Y-m-d-H-i-s') . '清洗数据完成,总耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');
